//放刘晶晶页面数据
var list=[]
import Mock from 'mockjs'
for(var i=0;i<100;i++){
    var phone  = Mock.mock({
        'regexp': /^1[3-9][\d]{9}/
      })
      var UserId=Mock.mock({
        'regexp': /\d{7}/
      })
      var state=Mock.mock({
        "boolean|1-2": true
      })
    list.push({
        id:Mock.mock('@guid()'),
        userId:UserId.regexp,
        name: Mock.mock('@cword(3, 9)'),
        tel:phone.regexp,
        money:Mock.mock('@integer(100, 10000)'),
        num:Mock.mock('@integer(1, 10)'),
        level:Mock.mock('@integer(1, 5)'),
        date:Mock.mock('@datetime()'),
        state:state.boolean
    })
}


function param2Obj(url){
    const search=url.split('?')[1]
    if(!search){
        return{}
    }
    return JSON.parse('{"'+decodeURIComponent(search)
    .replace(/"/g, '\\"')
    .replace(/&/g, '","')
    .replace(/=/g, '":"') + '"}')
    }  
    
  
    
    
export default{
    getLjjData:config=>{
        // console.log(config);
    var {keyword,page,pageSize} = param2Obj(config.url)
    var res = list.filter(item=>item.name.indexOf(keyword) != -1)
    //page:1,pageSize:20,下标为 0-19
    //page:2,pageSize:20,下标为20-39
    //(page-1)*pageSize <=index && < page * pageSize
    var resLimit=res.filter((item,index)=>{
        return  (page-1) * pageSize <= index && index< page * pageSize
        
    })
        return{
            code:200,
            msg:"请求成功",
            data:resLimit,
            total:list.length
        }
    },
    
    
    deleteId:config=>{
        var{id} = JSON.parse(config.body)
        // console.log('config',JSON.parse(config.body));
        // console.log('id',id);
        var res = list.filter(item=>item.id != id)
        list = res
        return{
            code:200,
            msg:"删除成功",
            data:null
        }
    } ,
    deleteManyById:config=>{
        console.log('config',JSON.parse(config.body));
        var {ids} = JSON.parse(config.body)
        console.log('ids',ids);
        var res = list.filter(item=>!ids.includes(item.id))
        list = res
        return{
            code:200,
            msg:"删除多个成功",
            data:null
        }

    } ,
    AddMessage:config=>{
        var {userId,name,tel,money,num,level}=JSON.parse(config.body)
        list.unshift({
            id:Mock.mock('@guid()'),
            userId,
            name,
            tel,
            money,
            num,
            level,
            date:Mock.mock('@datetime()'),            
        })
        return{
            code:200,
            msg:"新增成功",
            data:list
        }
    },
    UpdateMessage:config=>{
        var {id,userId,name,tel,money,num,level}=JSON.parse(config.body)         
    list.forEach(item=>{
        if(item.id==id){
            item.userId=userId
            item.name=name
            item.tel=tel
            item.money=money
            item.num=num
            item.level=level
            return
        }
    })
        return{
            code:200,
            msg:"更新成功",
            data:list
        }
    }

}





